package cn.loioi.web.controller.admin.api;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.loioi.multi.service.BaseAreaService;
import cn.loioi.multi.service.SysMessageService;
import cn.loioi.util.RequestUtil;
import cn.loioi.util.page.PageUtil;
import cn.loioi.web.common.BaseUserController;

/**
 * <br>
 * (c) Copyright 2016 by <br>
 * @description :通用后台接口 <br>
 * @file_name :APIContorller.java <br>
 * @system_name :multi_project_web <br>
 * @author :yyzh <br>
 * @创建时间 :2016年7月26日 下午1:42:33
 */
@RestController
@RequestMapping("/admin/api")
public class APIContorller extends BaseUserController {
	private @Resource SysMessageService sysMessageService;
	private @Resource BaseAreaService baseAreaService;

	/**
	 * 获取最新通知消息
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "notifications.json", method = RequestMethod.POST)
	public Model notifications(Model model) {
		Subject subject = getSubject();
		if (subject != null && (subject.isRemembered() || subject.isAuthenticated())) {
			// 获取当前登录用户信息
			model.addAttribute("user", getUser());
			// 获取未读消息总数
			model.addAttribute("total", sysMessageService.countForUnRead(getAccount()));
			// 获取最近5条未读消息
			sysMessageService.setPage(new PageUtil(1, 5));
			return renderSuccess(sysMessageService.selectByAccount(getAccount()), model);
		} else {
			return renderFail(model, "未登录");
		}
	}

	/**
	 * 获取区域列表
	 * @param code
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "area.json")
	public Model area(HttpServletRequest request, Model model) {
		String code = RequestUtil.getValueStr(request, "code");
		String name = RequestUtil.getValueStr(request, "name");
		return renderList(baseAreaService.selectForSelect2(code, name), model);
	}
}
